package com.ai.sb.controller;

import com.ai.sb.model.SbUser;
import com.ai.sb.service.SbUserService;

import com.ai.sb.util.DES;
import com.ai.sb.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


/**
* Created by CodeGenerator on 2018/06/10.
*/
@Api(value = "用户管理接口",tags = {"用户管理"})
@RestController
@RequestMapping("/sb/user")
public class SbUserController {
    private Logger logger = LoggerFactory.getLogger(SbUserController.class);
    @Resource
    private SbUserService sbUserService;

    @ApiOperation(value = "按用户Id查询" ,notes = "参数：id")
    @PostMapping("selUserById")
    public Result selUserById(@RequestBody SbUser sbUser){
        logger.info("查询用户信息，参数：{}",sbUser.getId());
        SbUser rs = sbUserService.findById(sbUser.getId());
        logger.info("查询结果：{}",rs);
        return Result.success(rs);
    }

    @ApiOperation(value = "按用户openId查询" ,notes = "参数：按用户openid查询")
    @PostMapping("selUserByOpenid")
    public Result selUserByOpenid(@RequestBody SbUser sbUser){
        logger.info("查询用户信息，参数：{}",sbUser.getOpenid());
        if(sbUser.getOpenid()==null){
            return Result.success(null);
        }
        SbUser rs = sbUserService.findBy("openid",sbUser.getOpenid());
        logger.info("查询结果：{}",rs);
        return Result.success(rs);
    }

    @ApiOperation(value = "用户初始化(app端调用)" ,notes = "参数: phone（加密的电话号）")
    @PostMapping("initUser")
    public Result initUser(@RequestBody SbUser sbUser){
        if(sbUser.getPhone()==null||"".equals(sbUser.getPhone())){
            return Result.fail("app登录手机号不存在");
        }
        try {
            String decryptDES = DES.decryptDES(sbUser.getPhone(), DES.key);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("手机号解析失败");
        }
        return sbUserService.initUser(sbUser);
    }


}
